﻿using MediatR;
using SmartProduction.Domain.yjh;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Write.API.Application.Commands.WorkFlow;

namespace SmartProduction.Write.API.Application.CommandHandlers.WorkFlow
{
    /// <summary>
    /// 工作流删除命令处理
    /// </summary>
    public class WorkFlowDeleteCommandHandler : IRequestHandler<WorkFlowDeleteCommand,APIResult<object>>
    {
        /// <summary>
        /// 工作流仓储
        /// </summary>
        private readonly IBaseRepository<Workflows> wokrfks;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="wokrfks">工作流仓储</param>
        public WorkFlowDeleteCommandHandler(IBaseRepository<Workflows> wokrfks)
        {
            this.wokrfks = wokrfks;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">实体</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public async Task<APIResult<object>> Handle(WorkFlowDeleteCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> reuslt = new APIResult<object>();

            var entity = wokrfks.GetAll().Result.FirstOrDefault(x => x.WorkflowsId == request.workflowsId);
            entity.IsDeleted = true;
            var r=wokrfks.UpdateAsync(entity).Result;

            if (r > 0)
            {
                LogModel logModel = new LogModel();
                logModel.CreateDate = DateTime.Now;
                logModel.CreatePerson = "YJH";
                logModel.LoginThing = "正在删除工作流";
                reuslt.Code = APIEnums.删除成功;
                reuslt.Msg = APIEnums.删除成功.ToString();
                reuslt.data = r;    
            }
            else
            {
                reuslt.Code = APIEnums.删除失败;
                reuslt.Msg = APIEnums.删除失败.ToString();
                reuslt.data = r;

            }

            return await Task.FromResult(reuslt);
        }
    }
}
